package cn.iocoder.yudao.module.member.api.job;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.api.job.dto.JobPageReqDTO;
import cn.iocoder.yudao.module.member.api.job.dto.JobRespDTO;
import cn.iocoder.yudao.module.member.api.jobView.JobViewApi;
import cn.iocoder.yudao.module.member.controller.admin.job.vo.MemberJobPageReqVO;
import cn.iocoder.yudao.module.member.convert.job.MemberJobConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyDO;
import cn.iocoder.yudao.module.member.dal.dataobject.job.MemberCompanyJobDO;
import cn.iocoder.yudao.module.member.dal.dataobject.job.MemberJobDO;
import cn.iocoder.yudao.module.member.service.company.CompanyService;
import cn.iocoder.yudao.module.member.service.job.MemberJobService;
import cn.iocoder.yudao.module.member.service.jobView.JobViewService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Validated
public class JobApiImpl implements JobApi {
    @Resource
    private MemberJobService memberJobService;
    @Resource
    private CompanyService companyService;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public JobRespDTO getJob(Long id) {
        MemberJobDO memberJobDO=memberJobService.getJob(id,1);
        return MemberJobConvert.INSTANCE.convert1(memberJobDO);
    }

    @Override
    public JobRespDTO getJob1(Long id) {
        MemberJobDO memberJobDO=memberJobService.getJob(id,0);
        return MemberJobConvert.INSTANCE.convert1(memberJobDO);
    }

    @Override
    public JobRespDTO getJobAll(Long id) {
        return MemberJobConvert.INSTANCE.convert1(memberJobService.getJob(id));
    }

    @Override
    public PageResult<JobRespDTO> getJobPage(JobPageReqDTO pageVO) {
        MemberJobPageReqVO jobPageReqVO=new MemberJobPageReqVO();
        BeanUtils.copyProperties(pageVO,jobPageReqVO);
        List list=new ArrayList<String>();
        list.add("2");
        PageResult<MemberJobDO> memberJobDOPageResult=memberJobService.getJobPage(jobPageReqVO,list);
        return MemberJobConvert.INSTANCE.convertPage1(memberJobDOPageResult);
    }

    @Override
    public PageResult<JobRespDTO> getJobPage1(JobPageReqDTO pageVO) {
        MemberJobPageReqVO request=new MemberJobPageReqVO();
        request.setIsNew(1);
        SortingField sortField=new SortingField ();
        sortField.setField("new_weight");
        sortField.setOrder("desc");
        request.setSortField(sortField);
        request.setPageSize(20);
        List<MemberJobDO> newList=new ArrayList<>();
        Integer pageSize=50;
        Integer totalSize=0;
        request.setStatus("2");
        List<MemberJobDO> hotJobList=memberJobService.getRankingJobList(request);
        List<Long> ids=new ArrayList<>();
        if(CollectionUtils.isNotEmpty(hotJobList)){
            for (MemberJobDO memberJobDO:hotJobList){
                ids.add(memberJobDO.getId());
                CompanyDO companyDO=companyService.getCompanyById(memberJobDO.getCompanyId());
                if(companyDO==null||StringUtils.isEmpty(companyDO.getCompanyLogo())||StringUtils.isEmpty(companyDO.getIndustry())){
                    continue;
                }
                memberJobDO.setCompanyLogo(companyDO.getCompanyLogo());
                memberJobDO.setIndustry(companyDO.getIndustry());
            }
            newList.addAll(hotJobList);
            totalSize=hotJobList.size();
            pageSize=pageSize-hotJobList.size();
        }
        MemberJobPageReqVO jobPageReqVO=new MemberJobPageReqVO();
       jobPageReqVO.setPageSize(pageSize);
        List list=new ArrayList<String>();
        list.add("2");
        List<MemberJobDO> list1 = memberJobService.getJobPage1(jobPageReqVO, list,ids);
        if(CollectionUtils.isNotEmpty(list1)){
            newList.addAll(list1);
            totalSize=totalSize+list.size();
        }
        PageResult<MemberJobDO> page=new PageResult<>();
        page.setTotal(totalSize.longValue());
        page.setList(newList);
        return MemberJobConvert.INSTANCE.convertPage1(page);
    }

    @Override
    public List<JobRespDTO> getJobViewPage(JobPageReqDTO pageVO) {
        LoginUser loginUser= SecurityFrameworkUtils.getLoginUser();
        MemberJobPageReqVO jobPageReqVO=new MemberJobPageReqVO();
        BeanUtils.copyProperties(pageVO,jobPageReqVO);
        jobPageReqVO.setStudentId(loginUser.getId());
        List<MemberJobDO> jobViewPage = memberJobService.getJobViewPage(jobPageReqVO);
        return MemberJobConvert.INSTANCE.convertList1(jobViewPage);
    }

    @Override
    public List<JobRespDTO> getHotJob() {
        List<MemberJobDO> memberJobDOS=memberJobService.getHotJob();
        return MemberJobConvert.INSTANCE.convertList1(memberJobDOS);
    }

    @Override
    public List<JobRespDTO> getHotJob1() {
        MemberJobPageReqVO request=new MemberJobPageReqVO();
        request.setIsHot(1);
        SortingField sortField=new SortingField ();
        sortField.setField("hot_weight");
        sortField.setOrder("desc");
        request.setSortField(sortField);
        request.setPageSize(20);
        List<MemberJobDO> newList=new ArrayList<>();
        Integer pageSize=50;
        request.setStatus("2");
        List<MemberJobDO> hotJobList=memberJobService.getRankingJobList(request);
        List<Long> ids=new ArrayList<>();
        if(CollectionUtils.isNotEmpty(hotJobList)){
            ids=hotJobList.stream().map(MemberJobDO::getId).collect(Collectors.toList());
            newList.addAll(hotJobList);
            pageSize=pageSize-hotJobList.size();
        }
        List<MemberJobDO> memberJobDOS=memberJobService.getHotJob(pageSize,ids);
        newList.addAll(memberJobDOS);
        if(CollectionUtils.isNotEmpty(newList)){
            newList.stream().forEach(m->{
                CompanyDO company  = companyService.getCompanyById(m.getCompanyId());
                if(company!=null && StringUtils.isNotBlank(company.getCompanyLogo())){
                    m.setCompanyLogo(company.getCompanyLogo());
                }
                if(company!=null &&StringUtils.isNotBlank(company.getIndustry())){
                    m.setIndustry(company.getIndustry());
                }
            });
        }
        return MemberJobConvert.INSTANCE.convertList1(newList);
    }

    @Override
    public List<JobRespDTO> getJobListTop2(Long id) {
        List<MemberCompanyJobDO>  memberCompanyJobDOList=memberJobService.getJobListTop2(id);
        return MemberJobConvert.INSTANCE.convertList2(memberCompanyJobDOList);
    }

    @Override
    public String isJob(Long id) {
        MemberJobDO memberJobDO=memberJobService.getJob(id,2);
        if(memberJobDO==null){
            return "2";
        }else if(memberJobDO.getStatus().equals("3")){
            return "1";
        }
        return "0";
    }

    @Override
    public List<JobRespDTO> getAllJobByIds(List<Long> ids) {
        return MemberJobConvert.INSTANCE.convertList1(memberJobService.getAllJobByIds(ids));
    }

    @Override
    public Long getCompanyJobCount(Long id) {
        MemberJobPageReqVO pageReqVO=new MemberJobPageReqVO();
        pageReqVO.setCompanyId(id);
        pageReqVO.setStatus("2");
        PageResult<MemberJobDO> result=memberJobService.getJobPage(pageReqVO);
        return result.getTotal();
    }

    @Override
    public Map<String,Long> getCompanyAdminCreatorPhone(Long id) {
        MemberJobDO memberJobDO=memberJobService.getJob(id,2);
        AdminUserRespDTO creatorUser= adminUserApi.getUser(memberJobDO.getUserId());
        CompanyDO companyDO=companyService.getCompany(memberJobDO.getCompanyId(),2) ;
        AdminUserRespDTO adminUser= adminUserApi.getUserByMobile(companyDO.getPhoneNumber(),memberJobDO.getCompanyId());

        Map<String,Long> phones=new HashMap<>();
        phones.put("creatorUser",creatorUser.getId());
        phones.put("adminUser",adminUser.getId());
        return phones;
    }

    @Override
    public Map<String, Long> getCompanyAdminCreatorPhone(Long jobId, Long companyId) {
        MemberJobDO memberJobDO=memberJobService.getJob(jobId,2);
        AdminUserRespDTO creatorUser= adminUserApi.getUser(memberJobDO.getUserId());
        CompanyDO companyDO=companyService.getCompany(companyId,2) ;
        AdminUserRespDTO adminUser= adminUserApi.getUserByMobile(companyDO.getPhoneNumber(),companyId);
        Map<String,Long> phones=new HashMap<>();
        phones.put("creatorUser",creatorUser.getId());
        phones.put("adminUser",adminUser.getId());
        return phones;
    }
}
